﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebUI.DAL;
using System.Data.Linq;

namespace WebUI.BL
{
    public class ProductBL
    {
        public void Insert(tbl_Product product)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_Product newproduct = new tbl_Product()
                    {
                        UserModied=product.UserModied,
                        ThirdCategoryNo=product.ThirdCategoryNo,
                        ThirdCategoryName=product.ThirdCategoryName,
                        Status=product.Status,
                        SecondCategoryNo=product.SecondCategoryNo,
                        SecondCategoryName=product.SecondCategoryName,
                        Season=product.Season,
                        Review=product.Review,
                        ProductName=product.ProductName,
                        ProductLink=product.ProductLink,
                        ProductCode=product.ProductCode,
                        Note=product.Note,
                        ModelNo=product.ModelNo,
                        ImgSmall=product.ImgSmall,
                        ImgReview=product.ImgReview,
                        ImgMedium=product.ImgMedium,
                        ImgLarge=product.ImgLarge,
                        GiftType=product.GiftType,
                        FirstCategoryNo=product.FirstCategoryNo,
                        FirstCategoryName=product.FirstCategoryName,
                        Description=product.Description,
                        DateModified=product.DateModified,
                        CreatedUser=product.CreatedUser,
                        CreatedDate=product.CreatedDate
                    };
                    contextdb.tbl_Products.InsertOnSubmit(newproduct);
                    contextdb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
        public void Update(tbl_Product product)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var updateproduct = contextdb.tbl_Products.SingleOrDefault(u => u.ProductNo == product.ProductNo);
                try
                {
                    if (updateproduct != null)
                    {
                        updateproduct.UserModied = product.UserModied;
                        updateproduct.ThirdCategoryNo = product.ThirdCategoryNo;
                        updateproduct.ThirdCategoryName = product.ThirdCategoryName;
                        updateproduct.Status = product.Status;
                        updateproduct.SecondCategoryNo = product.SecondCategoryNo;
                        updateproduct.SecondCategoryName = product.SecondCategoryName;
                        updateproduct.Season = product.Season;
                        updateproduct.Review = product.Review;
                        updateproduct.ProductName = product.ProductName;
                        updateproduct.ProductLink = product.ProductLink;
                        updateproduct.ProductCode = product.ProductCode;
                        updateproduct.Note = product.Note;
                        updateproduct.ModelNo = product.ModelNo;
                        updateproduct.ImgSmall = product.ImgSmall;
                        updateproduct.ImgReview = product.ImgReview;
                        updateproduct.ImgMedium = product.ImgMedium;
                        updateproduct.ImgLarge = product.ImgLarge;
                        updateproduct.GiftType = product.GiftType;
                        updateproduct.FirstCategoryNo = product.FirstCategoryNo;
                        updateproduct.FirstCategoryName = product.FirstCategoryName;
                        updateproduct.Description = product.Description;
                        updateproduct.DateModified = product.DateModified;
                        updateproduct.CreatedUser = product.CreatedUser;
                        updateproduct.CreatedDate = product.CreatedDate;

                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }

            }
        }
        public void Delete(int productNo)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var deleteproduct = contextdb.tbl_Products.SingleOrDefault(u => u.ProductNo == productNo);
                try
                {
                    if (deleteproduct != null)
                    {
                        contextdb.tbl_Products.DeleteOnSubmit(deleteproduct);
                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
        public void DeleteByStatus(int productNo)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var deleteproduct = contextdb.tbl_Products.SingleOrDefault(u => u.ProductNo == productNo);
                try
                {
                    if (deleteproduct != null)
                    {
                        deleteproduct.Status = "Deleted";
                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
    }
}
